package com.bootdo.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class IPUtils {
	private static Logger logger = LoggerFactory.getLogger(IPUtils.class);

	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	/**
	 * 获取IP地址
	 * 
	 * 使用Nginx等反向代理软件， 则不能通过request.getRemoteAddr()获取IP地址
	 * 如果使用了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP地址，X-Forwarded-For中第一个非unknown的有效IP字符串，则为真实IP地址
	 */
	public static String getIpAddr(HttpServletRequest request) {

		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
	}
	
	 public static	 class IpModel implements Serializable{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private Double lon;
		private Double lat;
		private String cityName;
		public Double getLon() {
			return lon;
		}
		public void setLon(Double lon) {
			this.lon = lon;
		}
		public Double getLat() {
			return lat;
		}
		public void setLat(Double lat) {
			this.lat = lat;
		}
		public String getCityName() {
			return cityName;
		}
		public void setCityName(String cityName) {
			this.cityName = cityName;
		}
		
		
		
	}
	
	public static IpModel getWzByIp(String ip) {
		
		try {
			String r =   null;
			if(StringUtils.isEmpty(ip)) {
				 r = HttpUtil.doGet("http://api.map.baidu.com/location/ip?ak=IPbGICDfxoG0vjpmGgC13ar4cajjgrvy&coor=bd09ll");
			}else {
				 r = HttpUtil.doGet("http://api.map.baidu.com/location/ip?ak=IPbGICDfxoG0vjpmGgC13ar4cajjgrvy&coor=bd09ll&ip="+ip);
			}
			JSONObject j = JSONObject.parseObject(r);
			if(j.getIntValue("status") == 0) {
				JSONObject contnet = j.getJSONObject("content");
				JSONObject p = contnet.getJSONObject("point");
				JSONObject c = contnet.getJSONObject("address_detail");
				IpModel m = new IpModel();
				m.setLat(p.getDouble("y"));
				m.setLon(p.getDouble("x"));
				m.setCityName(c.getString("city"));
				return m;
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	public static IpModel getByCityName(String name) {
		
		try {
			String r =   null;			
			r = HttpUtil.doGet("http://api.map.baidu.com/place/v2/search?query="+name+"&output=json&region="+name+"&ak=IPbGICDfxoG0vjpmGgC13ar4cajjgrvy");		
			//Hog.e(r);
			JSONObject j = JSONObject.parseObject(r);
			if(j.getIntValue("status") == 0) {
				JSONArray jsonArray = j.getJSONArray("results");
				if(null != jsonArray &&  jsonArray.size() > 0) {
					JSONObject job = jsonArray.getJSONObject(0);
					if(job.containsKey("location")) {
						JSONObject gps = job.getJSONObject("location");
						IpModel ipmodel = new IpModel();
						ipmodel.setLat(gps.getDouble("lat"));
						ipmodel.setLon(gps.getDouble("lng"));
						return ipmodel;
					}
					 
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	
	
	
	
	public static void  main(String []  atgs) throws Exception {
		/*String a = "ABCD";
		System.out.println(a.charAt(0));
		
		
		System.err.println(m.getLat()  +"-"+ m.getLon());*/
		//System.err.println( r );
		
		//System.err.println(IPUtils.getByCityName("永年市").getLat()); 
		// getCityNameByTx("石家庄","邯郸");
		
		IPUtils.IpModel m = IPUtils.getWzByIp(null);
		System.err.println(m.getCityName());
	}
	

}
